class Solution(object):
    def queryString(self, s, n):
        m = len(s)
        if n > 31 * m:
            return False
        p = [0] * n
        for i in range(m):
            if s[i] == '1':
                j = i
                num = 0
                while j < m:
                    num = 2 * num + int(s[j])
                    if num <= n:
                        p[num - 1] = 1
                        j += 1
                    else:
                        break
        return all(p)


data = Solution()
# s = '0110'
# n = 3
# print(data.queryString(s, n))
# s = '0110'
# n = 4
# print(data.queryString(s, n))
s = "1111000101"
n = 5
print(data.queryString(s, n))